#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


HAS_XSCRNSVR=0
X11_BIN=/usr/X11/bin
DESKTOP_SESSION=${DESKTOP_SESSION:-"xdm"}
MAIL=${MAIL-"/var/mail/$USER"}
SHELL=${SHELL-"/bin/sh"}
DISPLAY=${DISPLAY-":0"}
TERM=${TERM-"xterm"}

_ () {
  backup_TEXTDOMAIN=$TEXTDOMAIN
  backup_TEXTDOMAINDIR=$TEXTDOMAINDIR
  TEXTDOMAIN=gnome-session-2.0
  TEXTDOMAINDIR=/usr/share/locale
  export TEXTDOMAIN
  export TEXTDOMAINDIR

  if [ -x /usr/bin/gettext ] ; then
    gettext "$@"
  else
    printf "%s\n" "$@"
  fi

  TEXTDOMAIN=$backup_TEXTDOMAIN
  TEXTDOMAINDIR=$backup_TEXTDOMAINDIR
  export TEXTDOMAIN
  export TEXTDOMAINDIR
}

postrun_progress_update ()
{
  while [ 1 ] ; do
    I=1
    while [ $I -lt 99 ] ; do
      echo "$I"
      if [ `expr $I % 10` -eq 0 ] ; then
        /usr/lib/postrun-query -c JDS_wait -e;
        if [ $? -ne 0 ] ; then
          echo "100"
        fi
      fi
      I=`expr $I + 1`
      sleep 1
    done
  done
}


case $DESKTOP_SESSION in
gnome|GNOME|JDS|tgnome|TGNOME|tjds|TJDS|TrustedJDS)
    EDITOR=${EDITOR:-"gedit"}
    ;;
kde|KDE)
    EDITOR=${EDITOR:-"kedit"}
    ;;
*)
    EDITOR=${EDITOR:-"vi"}
    ;;
esac

for XPATH in `/bin/echo $PATH | /bin/tr ":" " "`
do
  if [ -x $XPATH/xscreensaver-demo ] ; then
    HAS_XSCRNSVR=1
  fi
done

if [ $HAS_XSCRNSVR -eq 0 -a -x $X11_BIN/xscreensaver-demo ] ; then
  PATH=${PATH}:$X11_BIN
fi

export PATH EDITOR MAIL TERM SHELL DISPLAY

if xmodmap | /usr/bin/grep mod4 | /usr/bin/grep Alt > /dev/null 2>/dev/null
then
    xmodmap -e "clear Mod1" \
            -e "clear Mod4" \
            -e "add Mod1 = Alt_L" \
            -e "add Mod1 = Alt_R" \
            -e "add Mod4 = Meta_L" \
            -e "add Mod4 = Meta_R"
fi

if [ -x /usr/lib/postrun-query ] ; then
  /usr/lib/postrun-query -c JDS_wait -e
  if [ $? -eq 0 ] ; then
    TEXT=`_ "Completing post install setup..."`
    postrun_progress_update |\
    zenity --progress --text "$TEXT" --percentage 0 --auto-close
    if [ $? -ne 0 ] ; then
      echo "Cancel the session"
      kill -9 $$
    fi
  fi
fi
